package di.scanner.handler;

import di.scanner.callback.Callback;
import di.scanner.callback.FileStoreCallback;
import di.scanner.io.FileReader;
import di.scanner.model.JavaClassElement;
import di.scanner.paser.JavaPaser;
import di.scanner.producer.FileProducer;
import di.scanner.store.FileStore;
import di.scanner.store.JavaClassStore;

import java.io.File;
import java.io.IOException;

public class JavaPaserHandler implements Handler{

    private JavaPaser paser;

    private Callback callback;

    public JavaPaserHandler() {
        paser = new JavaPaser();
        callback = new FileStoreCallback();
    }


    public void handle() {
        while (FileStore.getInstance().getCount()>0){
            String filePath = FileStore.getInstance().poll();
            if (filePath==null){
                return;
            }
            if (FileProducer.END_FILE.equals(filePath)){
                callback.callback();
                return;
            }
            try {
                String resource = FileReader.read(filePath);
                String moduleName = FileReader.getModuleName(filePath);
                String domainName = FileReader.getDomainName(filePath);
                JavaClassElement javaClassElement = paser.buildClass(resource,moduleName,domainName);
                if (javaClassElement!=null){
                    JavaClassStore.getInstance().add(javaClassElement);
                    //System.out.println(new File(filePath).getName()+" parser done");
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("error in reading file:"+filePath);
            }

        }
    }

}
